﻿using SMplusXTeam.SyncMoped.SyncGurke;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace SyncMoped
{
    class Program
    {
        private static String sourceDirectory = String.Empty;
        private static String backupDirectory = String.Empty;
        private static uint backupCount = 3;
        private static uint holdBackDays = 365;
        private static ComparisonMethod comparisonMethod = ComparisonMethod.Md5;
        private static bool shouldCompress = false;

        static void Main(string[] args)
        {
            try
            {
                ProcessParameters(args);

            }
            catch (Exception ex)
            {
                Console.WriteLine("Error occured: " + ex.Message);
                Environment.Exit(-1);
            }
            Environment.Exit(0);
        }

        private static void ProcessParameters(string[] args)
        {
            if (args.LongLength < 2)
            {
                PrintHelp();
            }
            else
            {
                foreach (String argument in args)
                {
                    if (argument.StartsWith("/?", StringComparison.InvariantCultureIgnoreCase))
                    {
                        PrintHelp();
                        Environment.Exit(0);
                    }
                    else if (argument.StartsWith("/C:", StringComparison.InvariantCultureIgnoreCase))
                    {
                        backupCount = Convert.ToUInt32(argument.Substring(3));
                    }
                    else if (argument.StartsWith("/H:", StringComparison.InvariantCultureIgnoreCase))
                    {
                        holdBackDays = Convert.ToUInt32(argument.Substring(3));
                    }
                    else if (argument.StartsWith("/M:", StringComparison.InvariantCultureIgnoreCase))
                    {
                        comparisonMethod = (ComparisonMethod)Enum.Parse(typeof(ComparisonMethod), argument.Substring(3), true);
                    }
                    else if (String.Compare(argument, "/Z", true) == 0)
                    {
                        shouldCompress = true;
                    }
                }
                sourceDirectory = args[args.LongLength - 2];
                if (!Directory.Exists(sourceDirectory))
                    throw new ArgumentException("Source directory is not existing!");
                backupDirectory = args[args.LongLength - 1];
                if (!Directory.Exists(backupDirectory))
                    Directory.CreateDirectory(backupDirectory);
            }
        }

        private static void PrintHelp()
        {
            Console.WriteLine("Backups a directory with versioning and hold-back-time activated.");
            Console.WriteLine();
            Console.WriteLine("SYNCMOPED [/C[[:]count]] [/H[[:]holdbackdays]] [/M[[:]method]] [/Z]" + System.Environment.NewLine + "          [sourcedrive:][path] [backupdrive:][path]");
            Console.WriteLine();
            Console.WriteLine("  [sourcedrive:][path]");
            Console.WriteLine("\t\tDefines the source directory");
            Console.WriteLine("  [backupdrive:][path]");
            Console.WriteLine();
            Console.WriteLine("\t\tDefines the backup directory");
            Console.WriteLine("  /C\t\tSets the maximum count of backup files");
            Console.WriteLine("  count\t\t The number of backup files, 3 is default.");
            Console.WriteLine("  /H\t\tSets the hold-back-time.");
            Console.WriteLine("  holdbackdays\t The number of days backup files are available," + Environment.NewLine + "\t\t 365     is default");
            Console.WriteLine("\t\t 0       means unlimited.");
            Console.WriteLine("  /M\t\tSets the comparison method.");
            Console.WriteLine("  method\t MD5     MD5-Sum comparison of files");
            Console.WriteLine("  \t\t Byte    A byte-wise comparsion of files.");
            Console.WriteLine("  /Z\t\tThe backup files should be compressed (ZIP)");
            Console.WriteLine();
        }
    }
}
